﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data;

namespace EasyBuildingCMS.BLL.CoreEngine
{
    public class EditableHTMLContentRepository : CoreEngineBaseRepository
    {
        public EditableHTMLContentRepository()
        {
            CacheKey += "_EditableHTMLContent";
        }

        #region Query

        public List<EditableHTMLContent> GetAcitvePages()
        {
            string key = CacheKey + "_Active";

            if (EnableCaching && Cache[key] != null)
            {
                return (List<EditableHTMLContent>)Cache[key];
            }

            this.CoreEngineContext.Pages.MergeOption = MergeOption.NoTracking;

            List<EditableHTMLContent> editableHTMLContents = (from content in CoreEngineContext.EditableHTMLContents
                                                             where content.Active==true
                                                             select content).ToList();

            if (EnableCaching)
            {
                CacheData(key, editableHTMLContents, CacheDuration);
            }

            return editableHTMLContents;
        }

        public EditableHTMLContent GetHTMLContentByID(int htmlContentID)
        {

            string key = string.Format(CacheKey + "_HTMLContent_{0}", htmlContentID);

            if (EnableCaching && (Cache[key] != null))
            {
                return (EditableHTMLContent)Cache[key];
            }

            //this.CoreEngineContext.Pages.MergeOption = MergeOption.NoTracking;
            EditableHTMLContent content = this.CoreEngineContext.EditableHTMLContents.Where(p => p.HTMLContentID == htmlContentID).FirstOrDefault<EditableHTMLContent>();

            if (EnableCaching)
            {
                CacheData(key, content, CacheDuration);
            }

            return content;
        }

        public EditableHTMLContent GetHTMLContentByBuildingBlockInstanceID(int buildingBlockInstanceID)
        {

            string key = string.Format(CacheKey + "_BuildingBlockInstance_{0}", buildingBlockInstanceID);

            if (EnableCaching && (Cache[key] != null))
            {
                return (EditableHTMLContent)Cache[key];
            }

            //this.CoreEngineContext.Pages.MergeOption = MergeOption.NoTracking;
            EditableHTMLContent content = this.CoreEngineContext.EditableHTMLContents.Where(p => p.BuildingBlockInstanceID == buildingBlockInstanceID).FirstOrDefault<EditableHTMLContent>();

            if (EnableCaching)
            {
                CacheData(key, content, CacheDuration);
            }

            return content;
        }

        #endregion

        #region Update

        public bool UpdateHTMLContent(int buildingBlockInstanceID, string content)
        {
            EditableHTMLContent editableHTMLContent = this.GetHTMLContentByBuildingBlockInstanceID(buildingBlockInstanceID);
            editableHTMLContent.HTMLContent = content;

            return base.SaveChange(buildingBlockInstanceID);
        }

        public bool UpdateFriendlyName(int buildingBlockInstanceID, string friendlyName)
        {
            EditableHTMLContent editableHTMLContent = this.GetHTMLContentByBuildingBlockInstanceID(buildingBlockInstanceID);
            editableHTMLContent.HTMLContentFriendlyName = friendlyName;

            return base.SaveChange(buildingBlockInstanceID);
        }

        public bool UpdateCssClass(int buildingBlockInstanceID, string cssClass)
        {
            EditableHTMLContent editableHTMLContent = this.GetHTMLContentByBuildingBlockInstanceID(buildingBlockInstanceID);
            editableHTMLContent.CssClass = cssClass;

            return base.SaveChange(buildingBlockInstanceID);
        }


        #endregion

        #region Insert

        public EditableHTMLContent InsertHTMLContent(int editableHTMLContentID, string content, string friendlyName, int buildingBlockID, string cssClass)
        {
            EditableHTMLContent editableHTMLContent = EditableHTMLContent.CreateEditableHTMLContent(editableHTMLContentID);
            editableHTMLContent.HTMLContent = content;
            editableHTMLContent.HTMLContentFriendlyName = friendlyName;
            editableHTMLContent.BuildingBlockInstanceID = buildingBlockID;
            editableHTMLContent.CssClass = cssClass;

            return this.InsertHTMLContent(editableHTMLContent);

        }

        public EditableHTMLContent InsertHTMLContent(EditableHTMLContent content)
        {

            try
            {
                if (content.EntityState == EntityState.Detached)
                {
                    this.CoreEngineContext.AddToEditableHTMLContents(content);
                }
                if (base.SaveChange(content.HTMLContentID))
                {
                    return content;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                ActiveExceptions.Add(CacheKey + "_" + content.HTMLContentID, ex);
                return null;

            }
        }

        #endregion

        #region " Delete Operations "

    

        #endregion

    }
}
